<?php
class Library_Model_NhanVienMapper extends Library_Model_DataMapperAbstract{
	private static $_instance = null;
	private $_paginator = null ;
	
	public function searchNhanVienById($id)
	{
		$db = $this->getDb();
		$selectNhanvien = $db->select()
		->from('tbl_nhanvien')
		->where('MaNhanVien = ?',$id);
		$nhanvien = $db->fetchRow($selectNhanvien);
	
		return $this->_populate($nhanvien);
	
	}
	public function nhanvienList()
	{
		$db = $this->getDb();
		$selectNhanvien = $db->select()
		->from('tbl_nhanvien');
		$nhanviens = $db->fetchAll($selectNhanvien);
	
		$nhanvienObjArray = array();
		foreach($nhanviens as $nhanvien){
			$chucvuMapper = Library_Model_ChucVuMapper::getInstance();
			$chucvu = $chucvuMapper->searchChucVuById($nhanvien['MaChucVu']);
			$nhanvien['tbl_chucvu'] = $chucvu;
			
			$phongbanMapper = Library_Model_PhongBanMapper::getInstance();
			$phongban = $phongbanMapper->searchPhongBanById($nhanvien['MaPhongBan']);
			$nhanvien['tbl_phongban'] = $phongban;
			
			$nhanvienObjArray[] = $this->_populate($nhanvien);
		}
	
		return $nhanvienObjArray;
	}
	
	public function _populate($data){
		$obj = new Library_Model_NhanVien();
		$obj->setId($data['MaNhanVien']);
		$obj->setMaphongban($data['MaPhongBan']);
		$obj->setMachucvu($data['MaChucVu']);
		$obj->setTennhanvien($data['TenNhanVien']);
		$obj->setNamsinh($data['NamSinh']);
		$obj->setDiachi($data['DiaChi']);
		$obj->setDienthoai($data['DienThoai']);
		$obj->setPhongban($data['tbl_phongban']);
		$obj->setChucvu($data['tbl_chucvu']);
		
		return $obj;
	}
	
	protected function _insert(Library_Model_DomainObjectAbstract $obj){
		$db = $this->getDb();
		$data =array(
				'MaNhanVien' 		=> $obj->getId(),
				'MaPhongBan'		=>$obj->getMaphongban(),
				'MaChucVu'			=>$obj->getMachucvu(),
				'TenNhanVien'		=>$obj->getTennhanvien(),
				'NamSinh'			=>$obj->getNamsinh(),
				'DiaChi'				=>$obj->getDiachi(),
				'DienThoai'			=>$obj->getDienthoai()
		);
		$db->insert('tbl_nhanvien',$data);
	}
	protected function _update(Library_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data =array(	
				'MaNhanVien' 		=> $obj->getId(),
				'MaPhongBan'		=>$obj->getMaphongban(),
				'MaChucVu'			=>$obj->getMachucvu(),
				'TenNhanVien'		=>$obj->getTennhanvien(),
				'NamSinh'			=>$obj->getNamsinh(),
				'DiaChi'				=>$obj->getDiachi(),
				'DienThoai'			=>$obj->getDienthoai()
		);
		$where = $db->quoteInto('MaNhanVien = ?', $obj->getId());
		$db->update('tbl_nhanvien',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaNhanVien = ?',$id);
		$db->delete('tbl_nhanvien',$where);
	}
	public static function getInstance(){
		if(!isset(self::$_instance)){
			$c = __CLASS__;
			self::$_instance = new $c;
		}
		return self::$_instance;
	}
}